var appPath = getAppPath();	

$(function(){
	initJspCompont();
	initJspGrid();
});

function initJspCompont(){
	$("#queryAuditStatus").combobox({
		url:appPath+'/system/code_getCodesForComboxDefaultAll.do?codeTypeId=1002',
		valueField:'id',
		textField:'text',
		onLoadSuccess:function(){
			$("#queryAuditStatus").combobox("setValue","0");
			initMemberGrid();
		}
	});
}

function initMemberGrid(){
	$("#memberGrid").datagrid({
		url:appPath+'/member/member_memberAuditPage.do',
		height:'full',
		striped: true,
		fitColumns:true,
		queryParams: getQueryParam(),
		singleSelect:true,
		checkbox:false,
		frozenColumns:[[
		      {field:'op',title:'操作',width:30,align:'center',
		    	  formatter:function(value,rowData,rowIndex){
		    		  if(rowData.auditStatus=="0")
		    			  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/images/system/icon/order-search.png' title='审核' onClick='showAudit("+rowIndex+")'/></a>";
		    		  else
		    			  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/images/system/icon/order-search.png' title='查看' onClick='showAudit("+rowIndex+",1)'/></a>";
		    	  }
		      }
		 ]],
		columns:[[
		      {field:'auditStatusName',title:'审核状态',width:60,align:'left',sortable:true},
		      {field:'createTime',title:'申请时间',width:110,align:'left',sortable:true,formatter:fmtCreateTime},
		      {field:'memberId',title:'会员ID',width:100,align:'left',sortable:true},
		      {field:'memberName',title:'会员名称',width:70,align:'left',sortable:true},
		      {field:'memberTypeName',title:'会员类型',width:70,align:'left',sortable:true},
		      {field:'identityCard',title:'身份证号码',width:100,align:'left',sortable:true},
		      {field:'email',title:'电子邮箱',width:100,align:'center',sortable:true},
		      {field:'mobilephone',title:'手机',width:70,align:'left',sortable:true},
		      {field:'address',title:'所属地区',width:200,align:'left',sortable:true}
		      ]],
		 pagination:true,
		 rownumbers:true
	});
}

function initJspGrid(){
	$("#auditHistory").datagrid({
		fitColumns: true,
		height:'auto',
		striped: true,
		columns:[[
		      {field:'auditor',title:'操作人',width:80,align:'left',sortable:true},
		      {field:'auditStatusName',title:'操作',width:80,align:'left',sortable:true},
		      {field:'auditOpinion',title:'审核意见',width:290,align:'left',sortable:true},
		      {field:'createTime',title:'操作时间',width:90,align:'left',sortable:true,formatter:fmtCreateTime}
		      ]],
		 rownumbers:true	
	});
}

/**
 * 展现审核界面
 * @param {} rowData
 */
function showAudit(rowIndex,viewFlg){
	//清理内容
	var rowData = $("#memberGrid").datagrid("getRows")[rowIndex];
	$('#addForm').form('clear');
	$("#auditOpinion").val("");
	$('#addForm').form('load',rowData);
	var setupDate = rowData.setupDate;
	if(setupDate&&setupDate.length>0){
		$("#setupDate").val($.DateUtil.format(new Date(setupDate),'yyyy/MM/dd'));
	}
	//插入内容
	showAuditHistory(rowData.memberId);
	
	$('#auditDlg').window({
			title:'会员信息审核',
			iconCls:'icon-orderdivide',
			modal: true,
			shadow: false,
			collapsible:false,
			minimizable:false,
			maximizable:false
		});
	if(viewFlg){
		$("#auditNotesPanel").hide();
		$("#auditBtnPanel").hide();
	}else{
		$("#auditNotesPanel").show();
	    $("#auditBtnPanel").show();
	}
	if(rowData.memberType=="1"){
		$("#personalTable").show();
		$("#companyTable").hide();
	}else{
		$("#personalTable").hide();
		$("#companyTable").show();
	}
	$('#auditDlg').window('move',{left:20,top:20});
	$('#auditDlg').window('open');
	
}

/**
 * 显示历史信息
 */
function showAuditHistory(memberId){
	$("#auditHisPanel").show();
	var url =appPath+'/member/member_showApplyHistory.do';
	var sendData = {};
	sendData.memberId = memberId;
	$.getJSON(url,sendData,function(data){
		if(data&&data.length>0){
			$("#auditHistory").datagrid("loadData",data);
		}else{
			$("#auditHisPanel").hide();
		}
	});
}

/**
 * 会员信息审核通过
 */
function auditPass(){
	$.messager.confirm('提示信息', '确认审核通过吗？', function(c) {
	if(c){
		var auditOpinion = $('#auditOpinion').val();
		var memberId = $('#memberId').val();
		var applyId = $('#applyId').val();
		$.ajax({
			url:appPath+"/member/member_memberAuditPass.do",
			data:{applyId:applyId,auditOpinion:auditOpinion,memberId:memberId},
			type:"post",
			success:function(data){
				if(data=='ok'){
					$.messager.alert('提示信息','已完成审核，该会员已成为认证会员！','info');
					doQuery();
					$('#auditDlg').window('close');
				}else{
					$.messager.alert('提示信息','抱歉，审核未能完成，'+data,'error');
					$('#auditDlg').window('close');
				}
			}
		});
		}
	});
}

/**
 * 会员信息审核不通过
 */
function auditBack(){
	var auditOpinion = $('#auditOpinion').val();
	if(auditOpinion == null || auditOpinion == ''){
		$.messager.alert('提示信息','请您在审核意见栏中写明不通过的原因！');
	}else{
		$.messager.confirm('提示信息', '确认审核不通过吗？', function(c) {
			if(c) {
				var memberId = $('#memberId').val();
				var applyId = $('#applyId').val();
				$.ajax({
					url:appPath+"/member/member_memberAuditBack.do",
					data:{applyId:applyId,auditOpinion:auditOpinion,memberId:memberId},
					type:"post",
					success:function(data){
						if(data=='ok'){
							$.messager.alert('提示信息','已完成审核,该会员信息审核不通过！');
							doQuery();
							$('#auditDlg').window('close');
						}else{
							$.messager.alert('提示信息','抱歉，审核未能完成，'+data,'error');
							$('#auditDlg').window('close');
						}
					}
				});
			}
		});
	}
}

/**
 * 结束编辑（关闭对话框）
 */
function cancelPostData(){
	$('#auditDlg').window('close');
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	var memberId = $("#queryMemberId").val();
	var memberName=$("#queryMemberName").val();
	var auditStatus=$("#queryAuditStatus").combobox("getValue");
	if(memberId&&memberId.length>0)queryObject.memberId = memberId;
	if(memberName&&memberName.length>0)queryObject.memberName = memberName;
	if(auditStatus&&auditStatus.length>0)queryObject.auditStatus = auditStatus;
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#memberGrid").datagrid('unselectAll');
	$("#memberGrid").datagrid('load',getQueryParam());
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.createTime=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}

function fmtCreateDate(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.createTime=$.DateUtil.format(s,'yyyy/MM/dd');
}
